﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.Utils.Configuration;

namespace Sustainalytics.Utils.Tests.Configuration
{
    [TestClass()]
    public class SettingsHelperTests
    {
        [TestMethod()]
        public void SettingsHelperTest()
        {

            // positives 
            Assert.AreEqual(SettingsHelper.Read<double>("double|0.5566"), (double).5566);
            Assert.AreEqual(SettingsHelper.Read<int>("int|100"), 100);
            Assert.AreEqual(SettingsHelper.Read<int>("int|-100"), -100);
            Assert.AreEqual(SettingsHelper.Read<string>("string|ABC"), "ABC");
            Assert.AreEqual(SettingsHelper.Read<DateTime>("datetime|2005.10.15 10:10:10"), DateTime.Parse("2005.10.15 10:10:10"));
            
           
            //missing 
            Assert.AreEqual(SettingsHelper.Read<int>("missing"), 0);
            Assert.AreEqual(SettingsHelper.Read<string>("missing"), null);
            Assert.AreEqual(SettingsHelper.Read<DateTime>("missing"), DateTime.MinValue);

            //default values 
            Assert.AreEqual(SettingsHelper.Read<int>("missing", 100), 100);
            Assert.AreEqual(SettingsHelper.Read<string>("missing", "blabla"), "blabla");
            Assert.AreEqual(SettingsHelper.Read<DateTime>("missing", DateTime.MaxValue), DateTime.MaxValue);

            //convertion failures, with exception supressed 
            Assert.AreEqual(SettingsHelper.Read<byte>("OverflowException"), 0);
            Assert.AreEqual(SettingsHelper.Read<DateTime>("InvalidCastException"), DateTime.MinValue);
            Assert.AreEqual(SettingsHelper.Read<DateTime>("ArgumentNullException"), DateTime.MinValue);
            Assert.AreEqual(SettingsHelper.Read<double>("FormatException"), .0);


            //convertion failures, with exception NOT supressed 
            try
            {
                Assert.AreEqual(SettingsHelper.Read<byte>("OverflowException", (byte)1, false), 1);
            }
            catch (OverflowException)
            {
            }

            try
            {
                Assert.AreEqual(SettingsHelper.Read<double>("FormatException", .8933, false), .89898);
            }
            catch (FormatException)
            {

            }

            //todo petre  search for a InvalidCastException
            Assert.IsTrue(true);

        }
    }
}
